#include <iostream>
#include <thread>
#include <chrono>


#include "PageManager.h"
#include "GuiContext.h"
#include "MachineDependents.h"
#include "LoopScheduler.h"
#include "CommandDispatcher.h"
#include "DmxDispatcher.h"
#include "ConfigManager.h"
#include "NetworkManager.h"
#include "FileDownloader.h"

#include "StatisticGatherer.h"
#include "RttyProcessManager.h"



using namespace std::chrono_literals;

void sigDebugger(int sig){
    std::cout << "catch signal:" << sig << std::endl;

}



int main() {

    std::cout << "starting firmware system...." << std::endl;
//    signal(SIGFPE, sigDebugger);
    ConfigManager& configManager = ConfigManagerSingleton::instance();

    std::cout << "firmware version:" << configManager.getVersionInfo().firmwareVersionString << std::endl;

    RttyProcessManagerSingleton::instance().killRuning();
    RttyProcessManagerSingleton::instance().runRtty();


    ExtendDeviceManagerSingleton::instance().initialize();

    PageManagerSingleton::instance();
    GuiContextSingleton::instance().run();
    StatisticGathererSingleton::instance().minfoPersistor.prepareForWrite();
    NetworkManagerSingleton::instance();
    TaskSchedulerSingleton::instance().getWorkThread().start();

    CommandDispatcher& commandDispatcher = CommandDispatcherSingleton::instance();
    commandDispatcher.mnetworkListeners.push_back(&LoopManagerSingleton::instance());
    commandDispatcher.getWorkThread().start();


//    INetwork network;
    LoopSchedulerSingleton::instance();//.offline(&network);

    DmxDispatcherSingleton::instance().getWorkThread().start();

    std::cout << "firmware system started!" << std::endl;

    longtimeTaskScheduler.getWorkThread().start();

//    NetworkManagerSingleton::instance().mlte4gNetwork.stopHardware();
//    NetworkManagerSingleton::instance().mlte4gNetwork.startHardware();

//    configManager.saveExtendDeviceConfig();

    std::string cmd;
    //  std::cin >> cmd;
    for (int i = 0 ; ; ++i) {//i < 100000
//        std::this_thread::sleep_for(300ms);
//    //    CommandDispatcherSingleton::instance().close(false);
//       NetworkManagerSingleton::instance().setActiveNetwork(NetworkType::IOT_4G);
//
//        std::this_thread::sleep_for(3ms);
// //       CommandDispatcherSingleton::instance().close(false);
//        NetworkManagerSingleton::instance().setActiveNetwork(NetworkType::ENTERNET1);
//
//        std::this_thread::sleep_for(3ms);
// //       CommandDispatcherSingleton::instance().close(false);
//        NetworkManagerSingleton::instance().setActiveNetwork(NetworkType::ENTERNET);


//        FileDownloaderSingleton::instance().getWorkThread().start();
//        FileDownloaderSingleton::instance().getWorkThread().join();

        std::this_thread::sleep_for(30s);

 //       ExtendDeviceManagerSingleton::instance().mextendHub.updateFirmware(MODULE_FIRMWARE_DIR + "/STM32HUB_V1.0.1_");

        NetworkString s;
//      s("<Z_videoControl_{\"videoId\":\"001\",\"speed\":\"5\",\"bright\":\"255\",\"mode\":\"1\"}>");
//      s = "<Z_uploadOfflineSystemInfo>";
//       s = "<Z_forwardData_{\"ip\":\"255.255.255.255\",\"port\":51000,\"timeout\":200,\"dataIsBinary\":true,\"data\":\"594D58000100000001000000000000006EA9759F\",\"haveResponse\":false,\"responseIsBinary\":true}>";
//        commandDispatcher.parseCommandExecute(s);

    }

    std::cout << "bye!" << std::endl;

    return 0;
}
